package com.moleq.service;

import java.util.ArrayList;
import java.util.List;

import android.R.integer;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.moleq.db.DatabaseHelper;
import com.moleq.db.DatabaseSchema;
import com.moleq.db.DatabaseSchema.ItemSetting;
import com.moleq.models.Departmant;
import com.moleq.models.Product;

public class ProductService {
	private DatabaseHelper DBHelper;

	public ProductService() {
	}

	public ProductService(Context context) {
		this.DBHelper = new DatabaseHelper(context);
	}

	public void Delete(long id) {
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		db.execSQL("DELETE FROM t_item_setting WHERE F01=?", new Object[] { id });
	}

	public Product Find(String barcode) {
		Product obj = null;
		SQLiteDatabase db = DBHelper.getWritableDatabase();

		Cursor cursor = db.rawQuery("SELECT *  FROM t_item_setting  Where  F01='" + barcode + "'", null);
		while (cursor.moveToNext()) {
			// long lid = cursor.getLong(cursor.getColumnIndex("P_ID"));
			String sBarcode = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemSetting.ITEM_UPC));
			String sName1 = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemSetting.DESCRIPTOR));
			String sDesc = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemSetting.EXPANDED_DESCRIPTION));
			double sDep = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.ItemSetting.DEPARTMENT_NUMBER));
			double dPrice = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.ItemSetting.UNIT_PRICE));
			double dCost = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.ItemSetting.CASE_COST));
			String iTaxable = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemSetting.TAX1));
			// String sImage =
			// cursor.getString(cursor.getColumnIndex("P_Image"));
			String sWeight = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemSetting.SCALE_OR_WEIGHT));
			boolean isWeight = false, btax1 = false, btax2 = false, btax3 = false, btax4 = false, btax5 = false, btax6 = false, 
					bfollowdep = false, bfoodstamp = false, isnodiscount = false,iscoupon = false,isWic = false,iscvv = false,
					isServiceItem = false;
			int currentPriceQty = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.ItemSetting.CURRENT_PRICE_QTY));
			if (sWeight.equals("1"))
				isWeight = true;
			// byte[] image = cursor.getBlob(cursor.getColumnIndex("ip_image"));
			String sfollowdep = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemSetting.FOLLOW_DEPARTMENT));
			double tax1 = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.ItemSetting.TAX1));
			double tax2 = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.ItemSetting.TAX2));
			double tax3 = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.ItemSetting.TAX3));
			double tax4 = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.ItemSetting.TAX4));
			double tax5 = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.ItemSetting.TAX5));
			double tax6 = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.ItemSetting.TAX6));
			String sfoodstamp = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemSetting.FOOD_STAMP));
			String isActive = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemSetting.ITEM_STATUS_INDICATOR));
			int sbottle_deposit = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.ItemSetting.BOTTLE_DEPOSIT));
			int age = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.ItemSetting.IS_AGE_VER));
			String nodiscount = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemSetting.ITEM_DISCOUNT_FLAG));
			double unit_price_factor = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.ItemSetting.UNIT_PRICE_FACTOR));
			String coupon = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemSetting.IS_COUPON));
			String wic = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemSetting.WIC_ELIGIBLE));
			String cvv = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemSetting.CVV_ELIGIBILITY));
			String service_fl = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemSetting.SERVICE_FL));
			int printer_id = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.ItemSetting.PRINTER_ID));
			String isMultCouponString = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemSetting.IS_MULT_COUPON));
			boolean isMultCoupon = false;
			if(isMultCouponString.equals("1")){
				isMultCoupon = true;
			}
			
			if (sfoodstamp.equals("1"))
				bfoodstamp = true;
			if (sfollowdep.equals("1"))
				bfollowdep = true;
			if (nodiscount.equals("1"))
				isnodiscount = true;
			if (coupon.equals("1")) {
				iscoupon = true;
			}
			if (service_fl.equals("1")) {
				isServiceItem = true;
			}
			if (wic.equals("1")) {
				isWic = true;
			}
			if (cvv.equals("1")) {
				iscvv = true;
			}
			
			if (tax1 > 0)
				btax1 = true;
			if (tax2 > 0)
				btax2 = true;
			if (tax3 > 0)
				btax3 = true;
			if (tax4 > 0)
				btax4 = true;
			if (tax5 > 0)
				btax5 = true;
			if (tax6 > 0)
				btax6 = true;
			
			obj = new Product(0, sBarcode, sName1, sDesc, dPrice, dCost, iTaxable, null, "0", isWeight, isActive, bfollowdep, sDep, btax1, btax2, btax3, btax4, btax5, btax6, bfoodstamp, sbottle_deposit, age,
					isnodiscount, unit_price_factor,currentPriceQty,iscoupon,isWic,iscvv,isServiceItem,printer_id,isMultCoupon);

		}

		cursor.close();
		return obj;
	}

	public List<Product> FindAll() {
		List<Product> obj = new ArrayList<Product>();
		SQLiteDatabase db = DBHelper.getWritableDatabase();

		Cursor cursor = db.rawQuery("SELECT * FROM t_item_setting", null);
		while (cursor.moveToNext()) {
			// long lid = cursor.getLong(cursor.getColumnIndex("P_ID"));
			String sBarcode = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemSetting.ITEM_UPC));
			String sName1 = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemSetting.DESCRIPTOR));
			String sDesc = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemSetting.EXPANDED_DESCRIPTION));
			double sDep = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.ItemSetting.DEPARTMENT_NUMBER));
			double dPrice = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.ItemSetting.UNIT_PRICE));
			double dCost = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.ItemSetting.CASE_COST));
			String iTaxable = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemSetting.TAX1));
			// String sImage =
			// cursor.getString(cursor.getColumnIndex("P_Image"));
			String sWeight = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemSetting.SCALE_OR_WEIGHT));
			boolean isWeight = false, btax1 = false, btax2 = false, btax3 = false, btax4 = false, btax5 = false, btax6 = false,
					bfollowdep = false, bfoodstamp = false, isnodiscount = false,iscoupon = false,isWic = false,iscvv = false,
					isServiceItem = false;
			int currentPriceQty = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.ItemSetting.CURRENT_PRICE_QTY));
			if (sWeight.equals("1"))
				isWeight = true;
			// byte[] image = cursor.getBlob(cursor.getColumnIndex("ip_image"));
			String sfollowdep = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemSetting.FOLLOW_DEPARTMENT));
			double tax1 = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.ItemSetting.TAX1));
			double tax2 = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.ItemSetting.TAX2));
			double tax3 = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.ItemSetting.TAX3));
			double tax4 = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.ItemSetting.TAX4));
			double tax5 = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.ItemSetting.TAX5));
			double tax6 = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.ItemSetting.TAX6));
			String sfoodstamp = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemSetting.FOOD_STAMP));
			String isActive = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemSetting.ITEM_STATUS_INDICATOR));
			int sbottle_deposit = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.ItemSetting.BOTTLE_DEPOSIT));
			int age = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.ItemSetting.IS_AGE_VER));
			String nodiscount = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemSetting.ITEM_DISCOUNT_FLAG));
			double unit_price_factor = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.ItemSetting.UNIT_PRICE_FACTOR));
			String coupon = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemSetting.IS_COUPON));
			String wic = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemSetting.WIC_ELIGIBLE));
			String cvv = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemSetting.CVV_ELIGIBILITY));
			String service_fl = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemSetting.SERVICE_FL));
			int printer_id = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.ItemSetting.PRINTER_ID));
			String isMultCouponString = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemSetting.IS_MULT_COUPON));
			boolean isMultCoupon = false;
			if(isMultCouponString.equals("1")){
				isMultCoupon = true;
			}
			
			if (sfoodstamp.equals("1"))
				bfoodstamp = true;
			if (sfollowdep.equals("1"))
				bfollowdep = true;
			if (nodiscount.equals("1"))
				isnodiscount = true;
			if (coupon.equals("1")) {
				iscoupon = true;
			}
			if (service_fl.equals("1")) {
				isServiceItem = true;
			}
			if (wic.equals("1")) {
				isWic = true;
			}
			if (cvv.equals("1")) {
				iscvv = true;
			}
			
			if (tax1 > 0)
				btax1 = true;
			if (tax2 > 0)
				btax2 = true;
			if (tax3 > 0)
				btax3 = true;
			if (tax4 > 0)
				btax4 = true;
			if (tax5 > 0)
				btax5 = true;
			if (tax6 > 0)
				btax6 = true;

			obj.add(new Product(0, sBarcode, sName1, sDesc, dPrice, dCost, iTaxable, null, "0", isWeight, isActive, bfollowdep, sDep, btax1, btax2, btax3, btax4, btax5, btax6, bfoodstamp, sbottle_deposit, age, 
					isnodiscount, unit_price_factor,currentPriceQty,iscoupon,isWic,iscvv,isServiceItem,printer_id,isMultCoupon));

		}

		cursor.close();

		return obj;
	}

	public void Insert(Product obj) {
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		db.execSQL("INSERT INTO t_item_setting(F01,F02,F03,F08,F30,F79,F81,F82,F178,F924) VALUES(?,?,?,?,?,?,?,?,?,?)", new Object[] { obj.getBarcode(), obj.getName1(), obj.getDesc(), "A", obj.getPrice(), 0, 0, 0, 0, 0 });

	}

	public void UpdatePrice(Product obj) {
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		db.execSQL("UPDATE t_item_setting SET F30=? WHERE F01=?", new Object[] {obj.getPrice(), obj.getBarcode() });
	}

	public long getDeptItemCount(int deptNo) {
		long count = 0;
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		String strSQL = "select count(*) from t_item_setting where F03 =" + deptNo;
		Cursor cursor = db.rawQuery(strSQL, null);

		if (cursor.moveToFirst()) {
			count = cursor.getInt(0);
		}
		cursor.close();
		return count;
	}

	public List<String> getDeptItems(int deptNo) {
		List<String> lst = new ArrayList<String>();
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		String strSQL = "select F01 from t_item_setting where " + ItemSetting.DEPARTMENT_NUMBER + " = " + deptNo;
		Cursor cursor = db.rawQuery(strSQL, null);
		while (cursor.moveToNext()) {
			lst.add(cursor.getString(0));
		}
		cursor.close();
		return lst;
	}

	public List<String> getDeptItems2(int deptNo) {
		List<String> lst = new ArrayList<String>();
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		String strSQL = "select F01 from t_item_setting where " + ItemSetting.DEPARTMENT_NUMBER + " = " + deptNo;
		Cursor cursor = db.rawQuery(strSQL, null);
		while (cursor.moveToNext()) {
			lst.add(cursor.getString(0));
		}
		cursor.close();
		return lst;
	}

}
